package com.dly.blog.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.dly.blog.content.RedisContent;
import com.dly.blog.domain.ro.PageRO;
import com.dly.blog.domain.vo.CountVO;
import com.dly.blog.domain.vo.content.ContentHotVO;
import com.dly.blog.domain.vo.content.ContentShowVO;
import com.dly.blog.utile.RedisUtils;
import com.dly.blog.utile.SecurityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dly.blog.controller.BaseController;
import lombok.AllArgsConstructor;
import com.dly.blog.domain.vo.content.ContentVO;
import com.dly.blog.domain.ro.content.ContentPageRO;
import com.dly.blog.domain.ro.content.ContentAddRO;
import com.dly.blog.domain.ro.content.ContentEditRO;
import com.dly.blog.service.ContentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.dly.blog.domain.vo.PageVO;
import com.dly.blog.msg.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author DLY
 * @since 2024-01-24 09:53:18
 */
@Tag(name = "文章相关")
@AllArgsConstructor
@RestController
@RequestMapping("/content/content")
public class ContentController extends BaseController {

    private final ContentService contentService;

    private final RedisUtils redisUtils;

    @PostMapping("/count")
    public ResponseEntity<R<CountVO>> getCount(){
        return operate(()-> this.successDate(contentService.getCount(SecurityUtils.getUserId())));
    }

    @PostMapping("/hot")
    public ResponseEntity<R<List<ContentHotVO>>> getHot(){
        return operate(()-> this.successDate(contentService.getHot()));
    }

    @PostMapping("/support/{id}/{isSupport}")
    @Operation(summary = "给文章点赞")
    public ResponseEntity<R<Void>> support(@PathVariable(value = "id") String id, @PathVariable(value = "isSupport") boolean isSupport){
        if(isSupport) {
            redisUtils.removeZSet(RedisContent.SUPPORT_SORT, id);
            redisUtils.removeSet(RedisContent.CONTENT_SUPPORT + id, SecurityUtils.getUserId());
        }else {
            redisUtils.addZSet(RedisContent.SUPPORT_SORT, id);
            redisUtils.addSet(RedisContent.CONTENT_SUPPORT + id, SecurityUtils.getUserId());
        }
        return operate(this::success);
    }

    @PostMapping("/favorite/{id}/{isFavorite}")
    @Operation(summary = "给文章收藏")
    public ResponseEntity<R<Void>> favorite(@PathVariable(value = "id") String id, @PathVariable(value = "isFavorite") boolean isFavorite){
        if(isFavorite){
            redisUtils.removeZSet(RedisContent.FAVORITE_SORT, id);
            redisUtils.removeSet(RedisContent.CONTENT_FAVORITE + id, SecurityUtils.getUserId());
        }else {
            redisUtils.addZSet(RedisContent.FAVORITE_SORT, id);
            redisUtils.addSet(RedisContent.CONTENT_FAVORITE + id, SecurityUtils.getUserId());
        }
        return operate(this::success);
    }

    @PostMapping(value = "/showPage")
    @Operation(summary = "获取文章信息分页列表")
    public ResponseEntity<R<PageVO<ContentShowVO>>> getContentShowPage(@RequestBody ContentPageRO contentPageRO){
        return operate(()-> this.successDate(contentService.getContentShowPage(contentPageRO)));
    }


    @PostMapping(value = "/page")
    @Operation(summary = "分页获取文章信息")
    public ResponseEntity<R<PageVO<ContentVO>>> getContentPage(@RequestBody ContentPageRO contentPageRO){
        return operate(()-> this.successDate(contentService.getContentPage(contentPageRO)));
    }

    @PostMapping(value = "/add")
    @Operation(summary = "添加文章信息")
    public ResponseEntity<R<Void>> addContent(@RequestBody ContentAddRO contentAddRO){
        contentService.addContent(contentAddRO);
        return operate(this::success);
    }

    @PostMapping(value = "/edit")
    @Operation(summary = "修改文章信息")
    public ResponseEntity<R<Void>> editContent(@RequestBody ContentEditRO contentEditRO){
        contentService.editContent(contentEditRO);
        return operate(this::success);
    }

    @PostMapping(value = "/get/{id}")
    @Operation(summary = "获取文章信息")
    public ResponseEntity<R<ContentVO>> getContent(@PathVariable(value = "id") String id){
        Long is = redisUtils.addSet(RedisContent.READ_NUM + id, SecurityUtils.getUserId());
        if(ObjectUtil.isNotNull(is) && is.equals(1L)) redisUtils.addZSet(RedisContent.READ_SORT, id);
        return operate(()-> this.successDate(contentService.getContent(id)));
    }

    @PostMapping(value = "/remove/{ids}")
    @Operation(summary = "删除文章信息")
    public ResponseEntity<R<Void>> removeContent(@PathVariable(value = "ids") String[] ids){
        contentService.removeContent(ids);
        return operate(this::success);
    }

}
